package com.yinhai.adapter;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.net.Uri;
import android.text.ClipboardManager;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ImageSpan;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.yinhai.activity.R;
import com.yinhai.activity.SMSDetailActivity;
import com.yinhai.model.MessageBean;
import com.yinhai.photo.utils.Photo;
import com.yinhai.util.NullUtils;

public class MessageBoxListAdapter extends BaseAdapter {
    
    private List<MessageBean> mbList;
    
    private Context ctx;
    
    private LinearLayout layout_father;
    
    private LayoutInflater vi;
    
    private LinearLayout layout_child;
    
    private TextView tvDate;
    
    private TextView tvText;
    
    public MessageBoxListAdapter(Context context, List<MessageBean> coll) {
        ctx = context;
        vi = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.mbList = coll;
    }
    
    public int getCount() {
        return mbList.size();
    }
    
    public Object getItem(int position) {
        return mbList.get(position);
    }
    
    public long getItemId(int position) {
        return position;
    }
    
    public View getView(int position, View convertView, ViewGroup parent) {
        
        MessageBean mb = mbList.get(position);
        int itemLayout = mb.getLayoutID();
        layout_father = new LinearLayout(ctx);
        vi.inflate(itemLayout, layout_father, true);
        
        layout_father.setBackgroundColor(Color.TRANSPARENT);
        layout_child = (LinearLayout)layout_father.findViewById(R.id.layout_bj);
        
        tvText = (TextView)layout_father.findViewById(R.id.messagedetail_row_text);
        if ("2".equals(mb.getType())) {
            try {
                
                String yy_str = "a\n" + (mb.getText() == null ? "" : mb.getText());
                SpannableString spannablepic = new SpannableString(yy_str);
                if (NullUtils.isNotEmpty(mb.getFilePath())) {
                    Bitmap bitmap =
                        Photo.zoomImg(getMmsImage(mb.getFilePath()), com.yinhai.util.Utils.dip2px(ctx, 100), ctx);
                    // 是彩信
                    if (bitmap != null && !bitmap.isRecycled()) {
                        ImageSpan imageSpan = new ImageSpan(ctx, bitmap);
                        
                        spannablepic.setSpan(imageSpan, 0, 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
                    }
                    
                }
                
                tvText.setText(spannablepic);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
        } else {
            tvText.setText(mb.getText());
        }
        // tvText.setText(mb.getText());
        
        tvDate = (TextView)layout_father.findViewById(R.id.messagedetail_row_date);
        tvDate.setText(mb.getDate());
        addListener(tvText, tvDate, layout_child, mb);
        return layout_father;
    }
    
    private Bitmap getMmsImage(String _id) {
        Uri partURI = Uri.parse("content://mms/part/" + _id);
        InputStream is = null;
        Bitmap bitmap = null;
        try {
            is = ctx.getContentResolver().openInputStream(partURI);
            bitmap = BitmapFactory.decodeStream(is);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                }
            }
        }
        return bitmap;
    }
    
    public void addListener(final TextView tvText, final TextView tvDate, LinearLayout layout_bj, final MessageBean mb) {
        
        layout_bj.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                
            }
        });
        
        layout_bj.setOnLongClickListener(new OnLongClickListener() {
            public boolean onLongClick(View v) {
                if (mb.getName() != null && !mb.getName().equals("")) {
                    // tvText.setTextColor(0xffffffff);
                    showListDialog(newtan, mb);
                }
                return true;
            }
        });
        
        layout_bj.setOnTouchListener(new OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                
                    case MotionEvent.ACTION_DOWN:
                        
                    case MotionEvent.ACTION_MOVE:
                        tvText.setTextColor(0xff696969);
                        break;
                    
                    default:
                        tvText.setTextColor(Color.BLACK);
                        break;
                }
                return false;
            }
        });
    }
    
    private String[] newtan = new String[] {"转发", "复制", "删除", "呼叫联系人"};
    
    private void showListDialog(final String[] arg, final MessageBean mb) {
        new AlertDialog.Builder(ctx).setTitle("操作选择").setItems(arg, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                switch (which) {
                
                    case 0:
                        Intent intent = new Intent(ctx, SMSDetailActivity.class);
                        intent.putExtra("smsContent", mb.getText());
                        ctx.startActivity(intent);
                        break;
                    
                    case 1:
                        ClipboardManager cmb = (ClipboardManager)ctx.getSystemService(ctx.CLIPBOARD_SERVICE);
                        cmb.setText(mb.getText());
                        break;
                    case 2:
                        int i = ctx.getContentResolver().delete(Uri.parse("content://sms"), "_id=" + mb.getId(), null);
                        if (i > 0) {
                            mbList.remove(mb);
                            notifyDataSetChanged();
                            Toast.makeText(ctx, "短信删除成功", Toast.LENGTH_SHORT).show();
                        }
                        break;
                    case 3:
                        Uri uri = Uri.parse("tel:" + mb.getName().replace("+86", ""));
                        Intent call = new Intent(Intent.ACTION_CALL, uri);
                        ctx.startActivity(call);
                        break;
                }
            }
        }).show();
    }
}
